<style scoped>
</style>

<template>

  <div>

    <Modal v-model="IsModalShow" :title="modalFormTitle" :mask-closable="false" @on-cancel="cancel" width="600">
      <Form ref="modalForm" :model="modalForm" :label-width="80" value=true style="padding: 3px 60px">
        <Form-item label="部门名" prop="depName" :rules="{required: true, message: '必填,1-10位字符',min:1,max:10, trigger:'blur',type:'string'}">
          <Input v-model="modalForm.depName"></Input>
        </Form-item>
        <Form-item label="所属省份" >
          <Input v-model="modalForm.province"></Input>
        </Form-item>
        <Form-item label="所属市" >
          <Input v-model="modalForm.city"></Input>
        </Form-item>
        <Form-item label="上级部门" v-if="modalForm.pid!==0">
          <Input v-model="adminDept.depName" readonly="true"></Input>
        </Form-item>
      </Form>
      <div slot="footer">
        <Button type="ghost" @click="cancel">关闭</Button>
        <Button type="primary" :loading="modalForm_loading" @click="saveForm('modalForm')">保存</Button>
      </div>
    </Modal>
  </div>

</template>

<script>
import { saveDept,getAdmin } from "./../../../api/getData";
export default {
  props: {
    parentForm: {
      type: Object,
      default: function() {
        return {
          id: "",
          province: "",
          city: "",
          depName: "",
          depType:1,
          pid:""
        };
      }
    },
    modalShow: {
      type: Boolean,
      default: true
    },
    modalFormTitle: {
      type: String,
      default: "添加部门"
    }
  },
  data() {
    return {
      IsModalShow: false,
      modalForm: {},
      modalForm_loading: false,
      adminDept:{id:1,depName:''}
    };
  },
  watch: {
    modalShow(curVal, oldVal) {
      this.modalForm = Object.assign(this.parentForm);
      this.modalForm.depType=1;
      this.IsModalShow = curVal;
      if(curVal){
        this.getAdminDept();
      }
    }
  },
  created() {},
  mounted() {},
  methods: {
    async getAdminDept(){
        let result = await getAdmin({});
        if(result.code==0){
          this.adminDept = result.data;
        }

    },
    cancel() {
      this.$emit("listenModalForm");
    },
    saveForm(name) {
      this.$refs[name].validate(async valid => {
        if (valid) {
          this.modalForm_loading = true;
          if( this.modalForm.pid==0){
            this.modalForm.pid=this.adminDept.id;
          }
          const params = this.modalForm;
          try {
            let result = await saveDept(params);

            if (result.code == 0) {
              this.$emit("listenModalForm");
              this.$emit("refreshTableList");
            }
          } catch (err) {
            console.log(err);
            this.$Message.error("服务器异常，稍后再试");
          }
          this.modalForm_loading = false;
        } else {
          this.$Message.error("表单验证失败!");
        }
      });
    }
  }
};
</script>
